package com.dlyk.config.handler;


import com.dlyk.result.CodeEnum;
import com.dlyk.result.R;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    DataIntegrityViolationException e;

    @ExceptionHandler
    public R handlerException(Exception e) {
        e.printStackTrace();
        return R.FAIL(e.getMessage());
    }

    /**
     * @description: 权限不足的异常处理
     * @params: [e]       
     * @return: 
     * @author: ajie
     * @date: 2024/8/7 20:47
     */
    @ExceptionHandler
    public R handlerException(AccessDeniedException e) {
        e.printStackTrace();
        return R.FAIL(CodeEnum.ACCESS_DENIED);
    }

    @ExceptionHandler
    public R handlerSQLException(DataAccessException e) {
        e.printStackTrace();
        return R.FAIL(CodeEnum.DATA_ACCESS_EXCEPTION);
    }
}
